import 'package:flutter/material.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

class RankSectionTabMenu extends StatefulWidget {
  final int curIndex;
  final List<String> tabTitles;
  final TabController? tabController;

  const RankSectionTabMenu({
    Key? key,
    required this.curIndex,
    required this.tabTitles,
    this.tabController,
  });

  @override
  State<StatefulWidget> createState() {
    return _RankSectionTabMenuState();
  }
}

class _RankSectionTabMenuState extends State<RankSectionTabMenu> {
  late int curIndex;
  final itemKeys = [];

  @override
  void initState() {
    super.initState();
    curIndex = widget.curIndex;
    widget.tabController?.addListener(_listener);
  }

  void _listener() {
    print('_listener ${widget.tabController?.offset}');
    int tabIndex = widget.tabController?.index ?? 0;
    if (tabIndex != curIndex) {
      curIndex = tabIndex;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return TabBar(
      controller: widget.tabController,
      tabs: List.generate(
        widget.tabTitles.length,
        (index) => Row(
          children: [
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 3),
              child: Text(widget.tabTitles[index]),
            ),
            if (index != widget.tabTitles.length - 1) 12.lineV,
          ],
        ),
      ),
      isScrollable: true,
      labelPadding: EdgeInsets.zero,
      labelStyle: const TextStyle(
        fontSize: 14,
        height: 1.5,
        color: Color(0xE5FFFFFF),
        fontWeight: FontWeight.w500,
      ),
      unselectedLabelStyle: TextStyle(
        fontSize: 14,
        height: 1.5,
        fontWeight: FontWeight.w400,
        color: Color(0x8CFFFFFF),
      ),
      indicator: BoxDecoration(),
    );
  }


  @override
  void dispose() {
    widget.tabController?.removeListener(_listener);
    super.dispose();
  }
}
